package org.liquidByte.simpleChat.server;

import java.net.UnknownServiceException;

import org.liquidByte.simpleChat.communication.RemoteCallHandler;
import org.liquidByte.simpleChat.dto.Message;
import org.liquidByte.simpleChat.dto.User;

class RemoteCallHandlerImpl implements RemoteCallHandler {
	private ChatService chatService;
	
	protected ChatService getChatService() {
		if (this.chatService == null) {
			this.chatService = new ChatService();
		}
		return this.chatService;
	}

	@Override
	public Object handleCall(Object operation, Object[] params) throws Exception {
		String strOperation = (String)operation;
		
		if (strOperation.equals("chat/login") && params.length > 0) {
			return this.getChatService().login((User)params[0]);
		}
		else if (strOperation.equals("chat/getOnlineList")) {
			return this.getChatService().getOnlineList();
		}
		else if (strOperation.equals("chat/logoff")) {
			this.getChatService().logoff();
			return null;
		}
		else if (strOperation.equals("chat/sendMessage")) {
			this.getChatService().sendMessage((Message)params[0]);
			return null;
		}
		else if (strOperation.equals("chat/setStatus")) {
			this.getChatService().setStatus((User)params[0]);
			return null;
		}
		else {
			throw new UnknownServiceException("no such service for operation " + strOperation);
		}
	}
}
